package com.juttec.freshbird.pet.base.zixunbasetab;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.juttec.freshbird.pet.R;
import com.juttec.freshbird.pet.ZixunContentActivity;
import com.juttec.freshbird.pet.base.BasePager;
import com.juttec.freshbird.pet.base.BaseTab;
import com.juttec.freshbird.pet.view.HorizontalViewPager;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.viewpagerindicator.CirclePageIndicator;

import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.LogRecord;

/**
 * Created by Administrator on 2015/10/26.
 */
public class ZixunLatedTab extends BaseTab {

    private ViewPager mViewPager;
    private ListView mListView;
    private CirclePageIndicator mIndicator;
    private int[] imageView = new int[]{R.drawable.com_6, R.drawable.com_7, R.drawable.com_8,
            R.drawable.com_9, R.drawable.ic_launcher};
    private android.os.Handler mHandler;

    public ZixunLatedTab(Activity activity) {
        super(activity);
    }

    //建议使用viewutils的viewinject方法注入控件
    //原因：当填充布局大于等于2时，各个view通过id寻找控件会出现混乱情况
    //例：本案例中有view、headview两个View视图，可能出现view寻找headview里面空间的情况
    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.zixun_lated_tab, null);
        View headView = View.inflate(mActivity, R.layout.list_header_topnews, null);
        mViewPager = (ViewPager) headView.findViewById(R.id.vp_zixun_tab);
        mListView = (ListView) view.findViewById(R.id.lv_zixun_tab);
        mIndicator = (CirclePageIndicator) headView.findViewById(R.id.zixun_topnews_incicator);
        ViewUtils.inject(this, headView);
        ViewUtils.inject(this, view);
        mListView.addHeaderView(headView);
        mViewPager.setAdapter(new ZixunTabAdapter());
        mListView.setAdapter(new ZixunListViewAdapter());
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, ZixunContentActivity.class);
                mActivity.startActivity(intent);
            }
        });
        mIndicator.setViewPager(mViewPager);
        mViewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("pet", "点解了");
            }
        });
        return view;
    }

    @Override
    public void initData() {
        //广告条轮播
        if(mHandler==null){
            mHandler=new Handler(){

                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    int currentItem = mViewPager.getCurrentItem();
                    if(currentItem<4){
                        currentItem++;
                    }else{
                        currentItem =0;
                    }
                    mViewPager.setCurrentItem(currentItem);//切换到下一界面
                    mHandler.sendEmptyMessageDelayed(0,3000);//继续延时3秒发消息，形成循环
                }

            };
            mHandler.sendEmptyMessageDelayed(0, 3000);// 延时3秒后发消息
        }

    }

    class ZixunListViewAdapter extends BaseAdapter {

        private BitmapUtils bitmapUtils;

        public ZixunListViewAdapter() {
            bitmapUtils = new BitmapUtils(mActivity);
//            bitmapUtils.configDefaultLoadFailedImage(R.drawable.com_53);
        }

        @Override
        public int getCount() {
            return 6;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(mActivity, R.layout.zixunlated_listview_item, null);
                holder = new ViewHolder();
                holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_zixunlated_title);
                holder.tvDate = (TextView) convertView.findViewById(R.id.tv_date);
                holder.tvRedu = (TextView) convertView.findViewById(R.id.tv_redu);
                holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_pic);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
//            holder.tvRedu.setText("");
//            holder.tvDate.setText("");
//            holder.tvTitle.setText("");
//
//            bitmapUtils.display(holder.ivPic,"");

            return convertView;
        }
    }

    static class ViewHolder {
        public TextView tvTitle;
        public TextView tvDate;
        public TextView tvRedu;
        public ImageView ivPic;
    }

    class ZixunTabAdapter extends PagerAdapter {

        private BitmapUtils mUtils;

        public ZixunTabAdapter() {
            mUtils = new BitmapUtils(mActivity);
            //xutils获取图片是设置的默认图片
//            mUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView image = new ImageView(mActivity);
            image.setImageResource(imageView[position]);
            image.setScaleType(ImageView.ScaleType.FIT_XY);//基于空间大小填充图片
//            mUtils.display(image, "链接地址");
            container.addView(image);
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mActivity, "mynameishanleilei" + position, Toast.LENGTH_SHORT).show();
                }
            });
            return image;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}